package humynn.sbd
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * Used to load and store the game settings.
	 */
	public class Settings
	{
		private static var _loader:URLLoader;
		
		private static var _rounds:uint;
		private static var _roundLength:uint;
		private static var _bugInterval:Number;
		private static var _bugIntervalMod:Number;
		private static var _bugStateDelay:Number;
		private static var _bugDamage:Number;
		
		/**
		 * Gets the number of rounds.
		 * 
		 * @returns The number of rounds.
		 */
		public static function get rounds():uint
		{
			return _rounds;
		}
		
		/**
		 * Gets the length of each round.
		 * 
		 * @returns The length of each round.
		 */
		public static function get roundLength():uint
		{
			return _roundLength;
		}
		
		/**
		 * Gets the interval between spawning stink bugs for wave one.
		 * 
		 * @returns The interval between stink bugs.
		 */
		public static function get bugInterval():Number
		{
			return _bugInterval;
		}
		
		/**
		 * Gets the interval modification for each wave after the first.
		 * 
		 * @returns The interval modification.
		 */
		public static function get bugIntervalMod():Number
		{
			return _bugIntervalMod;
		}
		
		/**
		 * Gets the delay between bug stages.
		 * 
		 * @returns The delay.
		 */
		public static function get bugStateDelay():Number
		{
			return _bugStateDelay;
		}
		
		/**
		 * Gets how fast a stink bug will damage a health bar.
		 * 
		 * @returns The stink bug damage.
		 */
		public static function get bugDamage():Number
		{
			return _bugDamage;
		}
		
		public function Settings()
		{
		}
		
		/**
		 * Loads the game settings from the settings.txt file.
		 */
		public static function load():void
		{
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, loaded);
			_loader.load(new URLRequest("data/settings.txt"));
		}
		
		/**
		 * Reads the game settings from the loaded settings.txt file.
		 * 
		 * @param e Ignored.
		 */
		private static function loaded(e:Event):void
		{
			var lines:Array = String(_loader.data).split("\r\n");
			
			for each(var line:String in lines)
			{
				var parts:Array = line.split("=");
				
				if(parts[0] == "rounds")
				{
					_rounds = uint(parts[1]);
				}
				else if(parts[0] == "roundLength")
				{
					_roundLength = uint(parts[1]);
				}
				else if(parts[0] == "bugInterval")
				{
					_bugInterval = Number(parts[1]);
				}
				else if(parts[0] == "bugIntervalMod")
				{
					_bugIntervalMod = Number(parts[1]);
				}
				else if(parts[0] == "bugStateDelay")
				{
					_bugStateDelay = Number(parts[1]);
				}
				else if(parts[0] == "bugDamage")
				{
					_bugDamage = Number(parts[1]);
				}
			}
		}
	}
}